if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
  //移动端代码
}else{
  //PC端代码
}
var share_filename ='';
var aliyunUrl = 'https://newland-json.oss-cn-fuzhou.aliyuncs.com';

//读取url参数
if(window.location.href.match('item')){
	item = getQueryVariable('item')
	name = getQueryVariable('name')
	id = getQueryVariable('id')
	ToMyClick(item,name,id)
}

//获取根目录信息
var path = aliyunUrl + "/items.json";
$.ajax({
	type:"get", //使用get方式
	url:path, //json文件相对于这个HTML的路径
	dataType:"json",
	success:function(data) {
		var items = data;
		var i=0;
		for(item in items){
			// console.log(items[i]['name']);
			var item_name = document.getElementById('item_name');
			var option = document.createElement('option');
			option.value=items[i];
			option.textContent=items[i];
			item_name.appendChild(option);
			i=i+1;
		}
	},
	error:function() {
		alert("请求失败");
	}
});

// 校验身份证
function isIdCard(idCard) {
	var idcard = document.getElementById('message')
	// 15位和18位身份证号码的正则表达式
	var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;

	// 如果通过该验证，说明身份证格式正确，但准确性还需计算
	if (regIdCard.test(idCard)) {
		if (idCard.length == 18) {
			var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
					5, 8, 4, 2); // 将前17位加权因子保存在数组里
			var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); // 这是除以11后，可能产生的11位余数、验证码，也保存成数组
			var idCardWiSum = 0; // 用来保存前17位各自乖以加权因子后的总和
			for (var i = 0; i < 17; i++) {
				idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
			}

			var idCardMod = idCardWiSum % 11;// 计算出校验码所在数组的位置
			var idCardLast = idCard.substring(17);// 得到最后一位身份证号码

			// 如果等于2，则说明校验码是10，身份证号码最后一位应该是X
			if (idCardMod == 2) {
				if (idCardLast == "X" || idCardLast == "x") {
					//alert("恭喜通过验证啦！");
					return true;
				} else {
					//alert("身份证号码错误！");
					idcard.style.display='block';
					idcard.innerText="身份证号码不正确!";
					setTimeout(function(){idcard.style.display = 'none'},3000);
					return false;
				}
			} else {
				// 用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
				if (idCardLast == idCardY[idCardMod]) {
					//alert("恭喜通过验证啦！");
					return true;
				} else {
					//alert("身份证号码错误！");
					idcard.style.display='block';
					idcard.innerText="身份证号码不正确!";
					setTimeout(function(){idcard.style.display = 'none'},3000);
					return false;
				}
			}
		}else{
			return true;
		}
	} else {
		idcard.style.display='block';
		idcard.innerText="身份证号码不正确!";
		setTimeout(function(){idcard.style.display = 'none'},3000);
		return false;
	}
}

// 提交事件
function MyClick(){
	var item_name = document.getElementById("item_name");
	var myinput_idCard = document.getElementById("myinput_idCard");
	var myinput_name = document.getElementById("myinput_name");
	var haveFind = document.getElementById('haveFind');
	var noFind = document.getElementById('message');
	var myinput_idCardVal= myinput_idCard.value;
	
	if(isIdCard(myinput_idCardVal)){
	// if(true){
		
		//检测身份证X尾号，统一为大写，能够让用户在输入时忽略大小写
		if(myinput_idCardVal.slice(-1) === 'x'){
			myinput_idCardVal = myinput_idCardVal.replace('x','X');
		}
		
		//访问oss中json文件，获取数据
		var path = aliyunUrl +'/'+encodeURIComponent(item_name.value)+'/'+myinput_idCard.value+myinput_name.value+'.json'
		$.ajax({
			type:"get", //使用get方式
			url:path, //json文件相对于这个HTML的路径
			dataType:"json",
			success:function(data) {
				var exam_name = document.getElementById('exam-name');
				exam_name.innerText = data['证书名称']
				var stu_name = document.getElementById('stu-name');
				stu_name.innerText = data['姓名']
				var idcard_num = document.getElementById('idcard-num');
				idcard_num.innerText = data['身份证号']
				var cert_data = document.getElementById('cert-data');
				cert_data.innerText = data['证书编号']
				var cert_unit = document.getElementById('cert-unit');
				cert_unit.innerText = data['颁证单位']
				var cert_date = document.getElementById('cert-date');
				cert_date.innerText = data['颁证日期']
				var QR_code = document.getElementById('QR-code');
				var pathurl = window.location.href;
				if(pathurl.match(item)){
					pathurl = pathurl.split('?')[0];
				}
				var currentUrl = pathurl+'?'+'item='+encodeURIComponent(item_name.value)+'&name='+encodeURIComponent(myinput_name.value)+'&id='+encodeURIComponent(myinput_idCardVal);
				// 参考https://blog.csdn.net/weixin_44232093/article/details/104248235
				new QRious({
				   element:QR_code,
				   size:135,
				   background:'white',
				   foreground:'black',
				   level:'L',	   
				   value:currentUrl
				})
				//证书下载链接				
				if(data['有无扫描件']=='有'){
					var cert_down_url = document.getElementById('cert-down-url');
					cert_down_url.href = aliyunUrl+'/'+encodeURIComponent(item_name.value)+'/'+encodeURIComponent(item_name.value)+"(扫描版证书)"+'/'+myinput_idCard.value+myinput_name.value+'.pdf';
					var cert_down = document.getElementById('cert-down');
					cert_down.style.display='table-row';
				}
				if(data['有无扫描件']=='无'){
					var cert_down = document.getElementById('cert-down');
					cert_down.style.display='none';
				}
				var page1 = document.getElementById('page1');
				var page2 = document.getElementById('page2');
				page1.style.display='none';
				page2.style.display='block';
			},
			error:function() {
				// alert("请求失败");
				noFind.style.display='block';
				noFind.innerText="未检索到对应证书，请咨询相关考务人员。";
			}
		});				
		
	}
}

function ToMyClick(item,name,id){
	//访问oss中json文件，获取数据
	var path = aliyunUrl +'/'+item+'/'+id+name+'.json'
	console.log(path)
	$.ajax({
		type:"get", //使用get方式
		url:path, //json文件相对于这个HTML的路径
		dataType:"json",
		success:function(data) {
			var exam_name = document.getElementById('exam-name');
			exam_name.innerText = data['证书名称']
			var stu_name = document.getElementById('stu-name');
			stu_name.innerText = data['姓名']
			var idcard_num = document.getElementById('idcard-num');
			idcard_num.innerText = data['身份证号']
			var cert_data = document.getElementById('cert-data');
			cert_data.innerText = data['证书编号']
			var cert_unit = document.getElementById('cert-unit');
			cert_unit.innerText = data['颁证单位']
			var cert_date = document.getElementById('cert-date');
			cert_date.innerText = data['颁证日期']
			var QR_code = document.getElementById('QR-code');
			var currentUrl = window.location.href
			new QRious({
			    element: QR_code,
			    value: currentUrl
			  })
			//证书下载链接				
			if(data['有无扫描件']=='有'){
				var cert_down_url = document.getElementById('cert-down-url');
				cert_down_url.href = aliyunUrl+'/'+item+'/'+item+"(扫描版证书)"+'/'+id+name+'.pdf';
				var cert_down = document.getElementById('cert-down');
				cert_down.style.display='table-row';
			}
			if(data['有无扫描件']=='无'){
				var cert_down = document.getElementById('cert-down');
				cert_down.style.display='none';
			}
			var page1 = document.getElementById('page1');
			var page2 = document.getElementById('page2');
			page1.style.display='none';
			page2.style.display='block';
		},
		error:function() {
			// alert("请求失败");
			noFind.style.display='block';
			noFind.innerText="未检索到对应证书，请咨询相关考务人员。";
		}
	});	
}

//处理输入框回车事件
document.addEventListener('keydown', onInputKeyUp);
function onInputKeyUp(e){
	if (e.keyCode === 13 ){//enter事件
		MyClick();
	}
}

//返回查询界面
function BackPage1(){
	var page1 = document.getElementById('page1');
	var page2 = document.getElementById('page2');
	page2.style.display='none';
	page1.style.display='block';
}

//JS 获取 URL参数
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}